<?php

namespace Frieza\Modules\Position\Controllers;

use Biz\model\advertisement\MHhzPositionStartApp;
use Biz\service\advertisement\apiModule\AppStart;
use Doraemon\model\Position\PositionBaseInfo;
use Doraemon\model\Position\PositionStartApp;
use Doraemon\service\position\AdmExtOperationModule;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Widgets\Form;
use Illuminate\Http\Request;


class AppStartController extends BaseController
{
    use BaseTrait;

    public $base_field_share = PositionBaseInfo::IS_NOT_SHARE;

    public function saveModule(Request $request, $baseInfoId)
    {
        $base = $request->input('base', []);
        $rules = $request->input('rules', []);
        $video_type = $request->input('video_type', '');
        $material_type = $request->input('material_type', 0);
        $load_limit = $request->input('load_limit', '');
        $full_screen = $request->input('full_screen', '1');
        $skip = $request->input('skip', '1');
        $click_white = $request->input('click_white', '0');
        $click_text = $request->input('click_text', AppStart::getDefaultClickText());
        $material_type_ad = $request->input('material_type_ad', []);
        $pic_data = [];
        switch ($material_type) {
            case PositionStartApp::MATERIAL_TYPE_PIC:
                $pic_id = $request->input('pic_id', []);
                $pic_id = current($pic_id);
                $material_id = $pic_id;
                $material_type = $material_type_ad[PositionStartApp::MATERIAL_TYPE_PIC] ?? PositionStartApp::MATERIAL_TYPE_PIC;
                if (empty($pic_id)) throw new \Exception('完美通用图片必须上传');
                $id = $base['id'] ?? 0;
                $copy_id = $base['copy_id'] ?? 0;
                $rules = array_map('current', $rules);
                $rules['check'] = 1;
                $img_size = $rules['img_size'] ?? 0;
                if (!(($id > 0 || $copy_id > 0) && $img_size == 0)) $this->rulesValid($rules, 0);
                break;
            case PositionStartApp::MATERIAL_TYPE_AD:
                $whole_pic_id = $request->input('whole_pic_id', []);
                $material_id = '';
                $id = $base['id'] ?? 0;
                $copy_id = $base['copy_id'] ?? 0;
                $material_type = $material_type_ad[PositionStartApp::MATERIAL_TYPE_AD] ?? PositionStartApp::MATERIAL_TYPE_PIC;
                foreach ($whole_pic_id as $k => $v) {
                    $rule = array_map(function ($v) use ($k) {
                        return $v[$k] ?? '';
                    }, $rules);
                    $img_size = $rule['img_size'] ?? 0;
                    $rule['check'] = 1;
                    if ($k == 0) {
                        $material_id = $v;
                        if (empty($v)) throw new \Exception('完美通用图片必须上传');
                    } else {
                        if (!empty($v)) $pic_data['pic_' . $k] = $v;
                    }
                    if (!(($id > 0 || $copy_id > 0) && $img_size == 0)) $this->rulesValid($rule, $k);
                }
                break;
            case PositionStartApp::MATERIAL_TYPE_VEDIO:
                $video = $request->input('video', '');
                $material_id = $video;
                break;
            default:
                throw new \Exception('未开发的素材类型');
                break;
        }
        if ($material_type != MHhzPositionStartApp::MATERIAL_TYPE_PIC) $skip = 1;
        $data = [
            'position_base_info_id' => $baseInfoId,
            'video_type' => $video_type,
            'material_type' => $material_type,
            'material_id' => $material_id,
            'load_limit' => $load_limit,
            'full_screen' => $full_screen,
            'skip' => $skip,
            'click_white' => $click_white,
            'click_text' => $click_text,
        ] + $pic_data;
        AdmExtOperationModule::addPositionStartApp($data);
    }

    public function extendList(Grid $grid)
    {
        $grid->column('load_limit', '加载超时时间')->display(function ($title, $column) {
            return $this->load_limit . "/" . ($this->skip == '1' ? '支持跳过' : '不支持跳过');
        });
        $grid->column('material_type_desc', '素材类型');
        $grid->column('material_id', '素材')->display(function ($title, $column) {
            if ($this->material_type == PositionStartApp::MATERIAL_TYPE_VEDIO) {
                return '<video class="kv-preview-data file-preview-video" controls="" style="width:213px;height:160px;">
<source src="'. $this->material_id .'" type="video/mp4">
<div class="file-preview-other">
<span class="file-other-icon"><i class="glyphicon glyphicon-file"></i></span>
</div>
</video>';
            } else {
                return $column->image();
            }
        });
    }

    public function beforeLinkForm(Form $form)
    {
        $form->text('load_limit', '加载超时时间')->placeholder('单位ms,可不填;默认时长3秒');
    }

    public function extendDefaultBaseInfo()
    {
        return [
            'click_white' => 0,
            'click_text' => AppStart::getDefaultClickText(),
            'material_type' => 1,
            'material_type_ad' => [
                1 => 1,
                2 => 1,
            ],
            'video_type' => PositionStartApp::VEDIO_PLAY,
        ];
    }

    public function beforeTitleForm(Form $form)
    {
        Admin::style('[for=whole_pic_id_0] { color:red; } [for=pic_id_0] { color:red; }');
        $form->radio('material_type', '素材类型')
            ->options([1 => '图片', 2 => '图片(多图适配)', 3 => '视频'])
            ->when(1, function (Form $form) {
                $form->radio('material_type_ad.1', '广告')->options(['1' => '否', '2' => '是'])->help('非广告展示3s;广告展示5s;');
                $form->radio('skip', '是否支持跳过')->options([1 => '支持', 2=> '不支持'])->default(1)->help("4.15版本开始支持");

                //展示3s，不支持跳过; 展示5s，2s后展示跳过按钮，支持跳过;
                $this->bannerForm2AppStart($form, 'pic_id', 0);

            })->when(2, function (Form $form) {
                $form->radio('material_type_ad.2', '广告')->options(['1' => '否', '2' => '是'])->help('非广告展示3s;广告展示5s;');

                //展示3s，不支持跳过; 展示5s，2s后展示跳过按钮，支持跳过;
                $this->bannerForm2AppStart($form, 'whole_pic_id', null);

            })->when(3, function (Form $form) {
                $form->videoUpload('video', '视频');
                $form->radio('full_screen', '视频展示方式')->options(PositionStartApp::FULL_SCREEN_MAP)->default(PositionStartApp::FULL_SCREEN_NORMAL);
                $form->radio('video_type', '视频播放类型')->options(PositionStartApp::getVedioTypeList([PositionStartApp::VEDIO_ORIGIN, PositionStartApp::VEDIO_PLAY]));
            });
        $form->radio('click_white', '按钮颜色')->options([0 => '黑色底色', 1 => '白色底色']);
        $form->select('click_text', '跳转文案')->options(AppStart::getClickText());
        $form->multiLinkSelect('base.link', '跳转链接');
    }
}
